package com.oocl.hw1.task2.utils;

import org.junit.Test;

public class RandomUtils {

	/**
	 * 可设置一个元素的概率的 随机数生成
	 * 
	 * @param from
	 * @param to
	 * @param params
	 * @param prop
	 * @return
	 */
	public static int getRandomIntBetween(int from, int to, int params,
			double prop) {
		int res = 0;

		if (from > to) {
			int temp = from;
			from = to;
			to = temp;
		}

		if (prop < 0)
			prop = 0;
		if (prop >= 1) {
			if (params <= to && params >= from)
				return params;
		}

		if (prop >= 1.0 / (to - from + 1)) {
			res = (int) (Math.random() * (to - from + 1)) + from;
			if (res != prop) {
				double x = (prop - 1.0 / (to - from + 1));
				x *= ((to - from + 1) * 1.0) / (to - from);

				double m = Math.random();
				if (m <= x) {
					res = params;
				}
			}
		} else {
			// /这里，就需要增大分母了
			double m = Math.random();
			res = (int) (Math.random() * (to - from + 1)) + from;
			if (m <= prop)
				res = params;
			else {
				while (res == params)
					res = (int) (Math.random() * (to - from + 1)) + from;
			}
		}
		return res;
	}
}
